$(document).ready(function(){
	const api = require("../modules/api.js");
	const Modal = require("../modules/modal.js");
	const order_content = env.getTemplate("h5/order_content.html");
	const shopCart = sessionStorage.getItem("shopCart") ? JSON.parse(sessionStorage.getItem("shopCart")) : {};
	const address = sessionStorage.getItem("address") ? JSON.parse(sessionStorage.getItem("address")) : {};
	$("#payPirce").text(shopCart.payPrice);
	
	shopCart.userName = address.name;
	shopCart.userPhone = address.phone;
	shopCart.province = address.province;
	shopCart.city = address.city;
	shopCart.area = address.area;
	shopCart.address = address.address;

	order_content.render({order: shopCart}, function(err, html){
		$("#content").append(html);
		componentDidMounted();
	});

	function componentDidMounted(){
		$(".address-section").click(function(){
			window.location.href = `/h5/address?id=${address.id}`;
		});
	};
	
	const modal = new Modal();
	// 点击下单
	$("#submit").click(function(){
		console.log("good");
		console.log(shopCart);

		let form = {
	        itemsStr: JSON.stringify(shopCart.items),
	        type: shopCart.type,
	        payPrice: shopCart.payPrice,
	        province: shopCart.province,
	        city: shopCart.city,
	        area: shopCart.area,
	        address: shopCart.address,
	        userName: shopCart.userName,
	        userPhone: shopCart.userPhone,
	        tradeType: "OFFLINE",
	        remarks: "",
	        freight: shopCart.freight
	    };
	    console.log(form);
		api.order_create(form).then(data=>{
			console.log(data);
			if(data.code == "0"){
				window.location.href = "/h5/success";
			}else{
				modal.confirm({title: "错误提示", content: "请稍后再试！"});
			};
		});
	});
})